<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="司机" prop="driver">
        <el-input
          v-model="queryParams.driver"
          placeholder="请输入司机"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="车牌号" prop="number">
        <el-input
          v-model="queryParams.number"
          placeholder="请输入车牌号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="客户电话" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入客户电话"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="客户地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入客户地址"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <!--<el-form-item label="注意事项" prop="care">
        <el-input
          v-model="queryParams.care"
          placeholder="请输入注意事项"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>-->
      <!--<el-form-item label="驾驶员id" prop="did">
        <el-input
          v-model="queryParams.did"
          placeholder="请输入驾驶员id"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>-->
      <!--<el-form-item label="配送状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择配送状态"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>-->
      <!--<el-form-item label="预计送达时间" prop="time">
        <el-date-picker
          v-model="queryParams.time"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>-->
      <!--<el-form-item label="是否加急处理true/false" prop="urgent">
        <el-select
          v-model="queryParams.urgent"
          placeholder="请选择是否加急处理true/false"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>-->
      <!--<el-form-item label="车辆id" prop="vid">
        <el-input
          v-model="queryParams.vid"
          placeholder="请输入车辆id"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>-->
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <!--<el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['wms:distribution:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>-->
        <!--<el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['wms:distribution:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>-->
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <!--<el-table-column label="主键" align="center" prop="id" />-->
      <el-table-column label="司机" align="center" prop="driver" />
      <el-table-column label="车牌号" align="center" prop="number" />
      <el-table-column label="客户电话" align="center" prop="phone" />
      <el-table-column label="客户地址" align="center" prop="address" />
      <el-table-column label="注意事项" align="center" prop="care" />
      <el-table-column label="预计送达时间" align="center" prop="time" :formatter="dateFormatter" width="180px" />
      <!--<el-table-column label="驾驶员id" align="center" prop="did" />-->
      <el-table-column label="配送状态" align="center" prop="statusDesc">
        <template #default="scope">
          <el-tag v-if="scope.row.status === 0" type="danger">等待审核</el-tag>
          <el-tag v-if="scope.row.status === 1">正在运输</el-tag>
          <el-tag v-if="scope.row.status === 2" type="success">配送完成</el-tag>
        </template>
      </el-table-column>
      <!--<el-table-column label="是否加急处理true/false" align="center" prop="urgent" />-->
      <!--<el-table-column label="车辆id" align="center" prop="vid" />-->
      <el-table-column label="创建时间" align="center" prop="createAt" />
      <el-table-column label="更新时间" align="center" prop="updateAt" />
      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['wms:distribution:update']"
          >
            编辑
          </el-button>
          <!--当配送状态为 0-等待审核 时，才能操作-->
          <span v-if="scope.row.status === 0">
            <el-popconfirm
              confirm-button-text="确认"
              cancel-button-text="取消"
              :icon="InfoFilled"
              icon-color="#626AEF"
              title="确认审核通过？"
              @confirm="confirmAudit(scope.row.id)"
              @cancel="cancelAudit(scope.row.id)"
            >
              <template #reference>
                <el-button
                  link
                  type="primary"
                  v-hasPermi="['wms:distribution:audit']"
                >
                  审核
                </el-button>
              </template>
            </el-popconfirm>
          </span>
          <!--当配送状态为 1-正在运输 时，才能操作-->
          <span v-if="scope.row.status === 1">
            <el-popconfirm
              confirm-button-text="确认"
              cancel-button-text="取消"
              :icon="InfoFilled"
              icon-color="#626AEF"
              title="确认配送完成？"
              @confirm="confirmOk(scope.row.id)"
              @cancel="cancelOk(scope.row.id)"
            >
            <template #reference>
              <el-button
                link
                type="primary"
                v-hasPermi="['wms:distribution:ok']"
              >
                配送
              </el-button>
            </template>
          </el-popconfirm>
          </span>
          <!--<el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['wms:distribution:delete']"
          >
            删除
          </el-button>-->
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <DistributionForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { DistributionApi, DistributionVO } from '@/api/wms/distribution'
import DistributionForm from './DistributionForm.vue'
import { InfoFilled } from '@element-plus/icons-vue'

/** 配送 列表 */
defineOptions({ name: 'Distribution' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<DistributionVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  address: undefined,
  care: undefined,
  did: undefined,
  driver: undefined,
  number: undefined,
  phone: undefined,
  status: undefined,
  time: undefined,
  urgent: undefined,
  vid: undefined,
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await DistributionApi.getDistributionPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: string) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: string) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await DistributionApi.deleteDistribution(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await DistributionApi.exportDistribution(queryParams)
    download.excel(data, '配送.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 审核确认按钮操作 */
const confirmAudit = async (id?: string) => {
  console.log('confirmAudit!',  id)
  try  {
    // 发送请求
    await DistributionApi.confirmAudit(id as string)
    message.success('审核已确认！')
  } catch {
  } finally {
    // 刷新列表
    handleQuery()
  }
}

/** 审核取消按钮操作 */
const cancelAudit = (id?: string) => {
  console.log('cancelAudit!', id)
}

/** 配送完成确认按钮操作 */
const confirmOk = async (id?: string) => {
  console.log('confirmOk!',  id)
  try  {
    // 发送请求
    await DistributionApi.confirmOk(id as string)
    message.success('配送完成已确认！')
  } catch {
  } finally {
    // 刷新列表
    handleQuery()
  }
}

/** 配送完成取消按钮操作 */
const cancelOk = (id?: string) => {
  console.log('cancelOk!', id)
}


/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
